
from rest_framework import serializers
from apps.common.base.serializers import BaseSerializer
from apps.chat.message.models import Message

class MessageBaseSerializer(BaseSerializer):

    class Meta:
        model = Message
        fields = "__all__"

class MessageModelSerializer(BaseSerializer):

    befrom = serializers.SerializerMethodField()
    def get_befrom(self, obj):
        if obj.befrom:
            data = {'id': obj.befrom.id }
            data['avatarUrl'] = obj.befrom.avatarUrl
            return data
        else:
            return {}
    
    isSelf = serializers.SerializerMethodField()
    def get_isSelf(self, obj):
        user = self.context["request"].user
        if obj.befrom == user:
            return True
        else:
            return False
            
    class Meta:
        model = Message
        fields = "__all__"
